﻿//-----------------------------------------------------------------------
// <copyright file="SettingsDefaultValueAttribute.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Settings
{
    using System;
    using System.Diagnostics;
    using TileTool.Common.Math;

    /// <summary>
    /// Default value attribute that is applied to a property to establish default and
    /// optional min / max values.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
    public class SettingsDefaultValueAttribute : Attribute
    {
        #region Fields - Private

        /// <summary>
        /// Default value.
        /// </summary>
        private object defaultValue = null;

        /// <summary>
        /// Min value - option can be null.
        /// </summary>
        private object minValue = null;

        /// <summary>
        /// Max value - option can be null.
        /// </summary>
        private object maxValue = null;

        #endregion

        #region Constructor - Float

        /// <summary>
        /// Default value constructor.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        public SettingsDefaultValueAttribute(float defaultValue)
        {
            this.Setup(defaultValue, null, null);
        }

        /// <summary>
        /// Default / min / max value constructor.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="minValue">Min value.</param>
        /// <param name="maxValue">Max value.</param>
        public SettingsDefaultValueAttribute(float defaultValue, float minValue, float maxValue)
        {
            this.Setup(defaultValue, minValue, maxValue);
        }

        #endregion

        #region Constructor - Int

        /// <summary>
        /// Default value constructor.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        public SettingsDefaultValueAttribute(int defaultValue)
        {
            this.Setup(defaultValue, null, null);
        }

        /// <summary>
        /// Default / min / max value constructor.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="minValue">Min value.</param>
        /// <param name="maxValue">Max value.</param>
        public SettingsDefaultValueAttribute(int defaultValue, int minValue, int maxValue)
        {
            this.Setup(defaultValue, minValue, maxValue);
        }

        #endregion

        #region Constructor - Color

        /// <summary>
        /// Color default value constructor.
        /// </summary>
        /// <param name="r">Red component.</param>
        /// <param name="g">Green component.</param>
        /// <param name="b">Blue component.</param>
        /// <param name="a">Alpha component.</param>
        public SettingsDefaultValueAttribute(byte r, byte g, byte b, byte a)
        {
            this.Setup(new ColorRGBA(r, g, b, a), null, null);
        }

        #endregion

        #region Constructor - bool

        /// <summary>
        /// Default value constructor.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        public SettingsDefaultValueAttribute(bool defaultValue)
        {
            this.Setup(defaultValue, null, null);
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the default value.
        /// </summary>
        public object DefaultValue
        {
            get
            {
                return this.defaultValue;
            }
        }

        /// <summary>
        /// Gets if we have a min / max value set.
        /// </summary>
        public bool HasMinMaxValue
        {
            get
            {
                return this.minValue != null;
            }
        }

        /// <summary>
        /// Gets the min value.
        /// </summary>
        public object MinValue
        {
            get
            {
                Debug.Assert(this.HasMinMaxValue, "Must have a min / max value.");

                return this.minValue;
            }
        }

        /// <summary>
        /// Gets the max value.
        /// </summary>
        public object MaxValue
        {
            get
            {
                Debug.Assert(this.HasMinMaxValue, "Must have a min / max value.");

                return this.maxValue;
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Clamp the passed value to our stored min / max range.
        /// </summary>
        /// <param name="value">Value to clamp.</param>
        /// <returns>Returns the clamped value.</returns>
        public object Clamp(object value)
        {
            if (!this.HasMinMaxValue)
            {
                return value;
            }

            IComparable minComp = (IComparable)this.minValue;
            IComparable maxComp = (IComparable)this.maxValue;

            if (minComp.CompareTo(value) > 0)
            {
                return minComp;
            }

            if (maxComp.CompareTo(value) < 0)
            {
                return maxComp;
            }

            return value;
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Setup the attribute.
        /// </summary>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="minValue">Min value. Can be null.</param>
        /// <param name="maxValue">Max value. Can be null.</param>
        private void Setup(object defaultValue, object minValue, object maxValue)
        {
            this.defaultValue = defaultValue;
            this.minValue = minValue;
            this.maxValue = maxValue;

            if (this.minValue != null)
            {
                if (this.minValue as IComparable == null)
                {
                    throw new ArgumentException("Min value must implement IComparable");
                }
            }

            if (this.maxValue != null)
            {
                if (this.maxValue as IComparable == null)
                {
                    throw new ArgumentException("Max value must implement IComparable");
                }
            }
        }

        #endregion
    }
}
